package com.huatech.common.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PropertiesUtil {

	private static Logger LOGGER = LoggerFactory.getLogger(PropertiesUtil.class);

	private final static String fileName = "Signatrue.properties";

	/**
	 * 
	 * 获取配置文件key值
	 * 
	 * @param pro
	 *            配置文件的key
	 * @return
	 * @version 1.0
	 */
	public static String getPro(String pro) {
		InputStream in = null;
		try {
			Properties prop = new Properties();
			in = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
			if (null == in)
				throw new RuntimeException("没有找到配置文件" + fileName);
			prop.load(in);
			in.close();
			return prop.getProperty(pro) == null ? "" : prop.getProperty(pro).trim();
		} catch (Exception ex) {
			RuntimeException rex = new RuntimeException(ex.getMessage());
			rex.setStackTrace(ex.getStackTrace());
			throw rex;
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (Exception ex) {
					RuntimeException rex = new RuntimeException(ex.getMessage());
					rex.setStackTrace(ex.getStackTrace());
					throw rex;
				}
			}

		}
	}

	// 根据Key读取Value
	public static String getValueByKey(String filePath, String key) {
		Properties properties = new Properties();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(new File(filePath)));
			properties.load(in);
			String value = properties.getProperty(key);
			return value;
		} catch (IOException e) {
			LOGGER.error("读取properties，error:", e);
			return null;
		}
	}

	// 读取Properties全部key\value
	@SuppressWarnings("rawtypes")
	public static Map<String, String> getAllProperties(String filePath) {
		Map<String, String> map = new HashMap<String, String>();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(new File(filePath)));
			Properties properties = new Properties();
			properties.load(in);
			// 得到配置文件的名字
			Enumeration en = properties.propertyNames();
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String value = properties.getProperty(key);
				map.put(key, value);
				LOGGER.info("初始化properties：" + key + "成功...");
			}
		} catch (Exception e) {
			LOGGER.error("读取properties，error:", e);
		}
		return map;
	}

	// 写入Properties信息
	public static void writeProperties(String filePath, String key, String value) {
		try {
			InputStream in = new FileInputStream(new File(filePath));
			Properties properties = new Properties();
			// 从输入流中读取属性列表（键和元素对）
			properties.load(in);

			OutputStream out = new FileOutputStream(filePath);
			properties.setProperty(key, value);
			// 以适合使用 load 方法加载到 Properties 表中的格式，
			// 将此 Properties 表中的属性列表（键和元素对）写入输出流
			properties.store(out, "insert key:" + key + ",value:" + value);
		} catch (Exception e) {
			LOGGER.error("insert properties 文件出错：", e);
		}
	}

}
